﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SMS.Entities.BaseClasses;
using SMS.Common.Silverlight;
using SMS.Controls.CategorySVC;
using SMS.Entities;
using SMS.Controls.SupplierSVC;

namespace SMS.Model
{
    public class SupplierModel
    {
        ServiceProxy<SupplierServiceClient> proxy = null;
        public event Action<int> OnSaveSupplierCompleted;
        public event Action<System.Collections.ObjectModel.ObservableCollection<Supplier>> OnSearchCompleted;
        public event Action<Supplier> OnGetCompleteSupplier;
        public event Action<SMSException> OnServiceError;
        public SupplierModel()
        {
            proxy = new ServiceProxy<SupplierServiceClient>("Service/SupplierService.svc");
            proxy.Client.SaveSupplierCompleted += new EventHandler<SaveSupplierCompletedEventArgs>(Client_SaveSupplierCompleted);
            proxy.Client.SearchSupplierCompleted += new EventHandler<SearchSupplierCompletedEventArgs>(Client_SearchSupplierCompleted);
            proxy.Client.GetCompleteSupplierCompleted += new EventHandler<GetCompleteSupplierCompletedEventArgs>(Client_GetCompleteSupplierCompleted);
        }

        void Client_GetCompleteSupplierCompleted(object sender, GetCompleteSupplierCompletedEventArgs e)
        {
            if (e.ServiceErrror == null)
            {
                if (OnGetCompleteSupplier != null)
                    OnGetCompleteSupplier(e.Result);
            }
            else if (OnServiceError != null)
                OnServiceError(e.ServiceErrror);
        }

        void Client_SearchSupplierCompleted(object sender, SearchSupplierCompletedEventArgs e)
        {
            if (e.serviceError == null)
            {
                if (OnSearchCompleted != null)
                    OnSearchCompleted(e.Result);
            }
            else if (OnServiceError != null)
                OnServiceError(e.serviceError);
        }

        void Client_SaveSupplierCompleted(object sender, SaveSupplierCompletedEventArgs e)
        {
            if (e.serviceError == null)
            {
                if (OnSaveSupplierCompleted != null)
                    OnSaveSupplierCompleted(e.Result);
            }
            else if (OnServiceError != null)
                OnServiceError(e.serviceError);
        }

        public void SaveSupplier(Supplier supplier)
        {
            proxy.Client.SaveSupplierAsync(App.LoggedinUserName, supplier);
        }

        public void SearchSupplier(string searchText)
        {
            proxy.Client.SearchSupplierAsync(App.LoggedinUserName, searchText);
        }

        public void GetCompleteSupplier(int id)
        {
            proxy.Client.GetCompleteSupplierAsync(App.LoggedinUserName, id);
        }
    }
}
